package com.cw.jeeyt.service.impl.api;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.cw.jeeyt.common.Order;
import com.cw.jeeyt.service.bo.api.ApiFacadeObjBo;
import com.cw.jeeyt.service.bo.api.ApiFacadeObjParamBo;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.api.ApiFacadeObjDao;
import com.cw.jeeyt.dao.model.api.ApiFacadeObjDo;
import com.cw.jeeyt.service.intf.api.ApiFacadeObjParamService;
import com.cw.jeeyt.service.intf.api.ApiFacadeObjService;
import com.cw.jeeyt.service.converter.api.ApiFacadeObjConverter;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 接口方法参数对象业务实现类
 *
 * @author chenw
 * @version 1.0.0
 */
@Service
public class ApiFacadeObjServiceImpl implements ApiFacadeObjService {

    private static final Logger logger = LoggerFactory.getLogger(ApiFacadeObjServiceImpl.class);
    @Autowired
    private ApiFacadeObjDao apiFacadeObjDao;//接口方法参数对象数据库操作接口
    @Autowired
    private ApiFacadeObjParamService apiFacadeObjParamService;//接口方法参数对象参数业务接口

    /**
     * 查询所有接口方法参数对象
     */
    @Override
    public List<ApiFacadeObjBo> queryApiFacadeObjList() throws Exception {
        return ApiFacadeObjConverter.doList2BoList(apiFacadeObjDao.queryApiFacadeObjList());
    }

    /**
     * 分页查询接口方法参数对象
     *
     * @param apiFacadeObjBo
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<ApiFacadeObjBo> queryByPage(ApiFacadeObjBo apiFacadeObjBo, Integer pageNo, Integer pageSize,
        Order order) throws Exception {
        PageInfo<ApiFacadeObjDo> pageInfo = apiFacadeObjDao.queryByPage(ApiFacadeObjConverter.bo2Do(apiFacadeObjBo),
            pageNo, pageSize, order);
        return new PageInfo<>(pageInfo, ApiFacadeObjConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据主键查寻接口方法参数对象信息
     *
     * @param id
     * @return
     */
    @Override
    public ApiFacadeObjBo getApiFacadeObjById(Long id) throws Exception {

        if (null == id) {
            logger.error("--主键id为空--");
            return null;
        }
        ApiFacadeObjBo apiFacadeObjBo = ApiFacadeObjConverter.do2Bo(apiFacadeObjDao.getById(id));
        //查询参数对象的参数信息
        List<ApiFacadeObjParamBo> apiFacadeObjParamList = apiFacadeObjParamService.queryListByObjId(
            apiFacadeObjBo.getId());
        if (apiFacadeObjParamList != null && !apiFacadeObjParamList.isEmpty()) {
            apiFacadeObjBo.setApiFacadeObjParamList(apiFacadeObjParamList);
        }
        return apiFacadeObjBo;
    }

    /**
     * 新增接口方法参数对象
     *
     * @param apiFacadeObjBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long insert(ApiFacadeObjBo apiFacadeObjBo) throws Exception {

        if (apiFacadeObjBo == null) {
            logger.error("接口方法参数对象为空");
            return null;
        }
        //校验当前包路径下是否已存在接口方法参数对象
        ApiFacadeObjBo checkApiFacadeObj = this.queryByCondition(apiFacadeObjBo.getObjCode(),
            apiFacadeObjBo.getObjPackage(), apiFacadeObjBo.getObjType());
        if (checkApiFacadeObj != null) {
            throw new Exception("当前包路径下的参数对象已存在,请确认");
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjDo apiFacadeObj = ApiFacadeObjConverter.bo2Do(apiFacadeObjBo);
        apiFacadeObj.setCreateBy(userVo.getId());
        apiFacadeObj.setCreateDate(new Date());
        apiFacadeObj.setUpdateBy(userVo.getId());
        apiFacadeObj.setUpdateDate(new Date());
        Integer result = apiFacadeObjDao.insert(apiFacadeObj);
        ApiFacadeObjBo facadeObj = this.queryByCondition(apiFacadeObjBo.getObjCode(), apiFacadeObjBo.getObjPackage(),
            apiFacadeObjBo.getObjType());
        return facadeObj.getId();
    }

    /**
     * 根据包路径、参数对象编码、参数对象类型查询接口方法参数对象
     *
     * @param objCode
     * @param objPackage
     * @param objType
     * @return
     */
    @Override
    public ApiFacadeObjBo queryByCondition(String objCode, String objPackage, String objType) {

        if (StringUtils.isEmpty(objCode) && StringUtils.isEmpty(objPackage) && StringUtils.isEmpty(objType)) {
            logger.error("--查询入参为空--");
            return null;
        }
        ApiFacadeObjBo apiFacadeObjBo = ApiFacadeObjConverter.do2Bo(
            apiFacadeObjDao.queryByCondition(objCode, objPackage, objType));
        return apiFacadeObjBo;
    }

    /**
     * 新增接口方法参数对象不为空的字段
     *
     * @param apiFacadeObjBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer insertSelective(ApiFacadeObjBo apiFacadeObjBo) throws Exception {

        if (apiFacadeObjBo == null) {
            logger.error("接口方法参数对象为空");
            return null;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjDo apiFacadeObj = ApiFacadeObjConverter.bo2Do(apiFacadeObjBo);
        apiFacadeObj.setCreateBy(userVo.getId());
        apiFacadeObj.setCreateDate(new Date());
        apiFacadeObj.setUpdateBy(userVo.getId());
        apiFacadeObj.setUpdateDate(new Date());
        Integer result = apiFacadeObjDao.insertSelective(apiFacadeObj);
        return result;
    }

    /**
     * 更新接口方法参数对象
     *
     * @param apiFacadeObjBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(ApiFacadeObjBo apiFacadeObjBo) throws Exception {

        if (apiFacadeObjBo == null) {
            logger.error("接口方法参数对象为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjDo apiFacadeObj = ApiFacadeObjConverter.bo2Do(apiFacadeObjBo);
        apiFacadeObj.setUpdateBy(userVo.getId());
        apiFacadeObj.setUpdateDate(new Date());

        apiFacadeObjDao.update(apiFacadeObj);
    }

    /**
     * 更新接口方法参数对象不为空的字段
     *
     * @param apiFacadeObjBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSelective(ApiFacadeObjBo apiFacadeObjBo) throws Exception {

        if (apiFacadeObjBo == null) {
            logger.error("接口方法参数对象为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        if (userVo == null) {
            logger.error("用户未登录");
            throw new Exception("用户未登录");
        }
        ApiFacadeObjDo apiFacadeObj = ApiFacadeObjConverter.bo2Do(apiFacadeObjBo);
        apiFacadeObj.setUpdateBy(userVo.getId());
        apiFacadeObj.setUpdateDate(new Date());

        apiFacadeObjDao.updateSelective(apiFacadeObj);
    }

    /**
     * 删除接口方法参数对象
     *
     * @param apiFacadeObjBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(ApiFacadeObjBo apiFacadeObjBo) throws Exception {

        if (apiFacadeObjBo == null) {
            logger.error("接口方法参数对象为空");
            return;
        }
        apiFacadeObjDao.delete(ApiFacadeObjConverter.bo2Do(apiFacadeObjBo));
    }
}
